using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class BackpackUI : MonoBehaviour
{
    private GameObject parentUI;
    private GameObject state;

    public List<SlotUI> slotuiList;

    private Transform Backpackui;

    private int PanelValue=0;

    private void Awake()
    {
        parentUI = transform.Find("ParentUI").gameObject;
        state = transform.Find("state").gameObject;
    }

    private void Start()
    {
        InitUI();
        Backpackui = GetComponent<Transform>();
        parentUI.SetActive(false);
        state.SetActive(false);
       
    }



    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Tab))
        {
            ToggleUI();
        }

       ChangUIPanel();

    }

    void InitUI()
    {
        slotuiList = new List<SlotUI>(new SlotUI[27]);
        SlotUI[] slotuiArray = transform.GetComponentsInChildren<SlotUI>();

        foreach(SlotUI slotUI in slotuiArray)
        {
            slotuiList[slotUI.index] = slotUI;
        }
        UpdateUI();
    }

    void ChangUIPanel()
    {
        if (Input.GetKeyDown(KeyCode.E))
        {
             switch (PanelValue)
            {
                case 0:
                    Backpackui.GetChild(0).gameObject.SetActive(false);
                    Backpackui.GetChild(1).gameObject.SetActive(true);
                    PanelValue = 1;
                    break;
                case 1:
                    Backpackui.GetChild(1).gameObject.SetActive(false);
                    Backpackui.GetChild(0).gameObject.SetActive(true);
                    PanelValue = 0;
                    break;
            }
        }
    }
    public void UpdateUI()
    {
        List<SlotData> slotDataList = Inventorymanager.Instance.Backpack.slotList;
        for(int i = 0;i < slotDataList.Count; i++)
        {
            slotuiList[i].SetData(slotDataList[i]);
        }
    }

    private void ToggleUI()
    {
        parentUI.SetActive(!parentUI.activeSelf);
    }


    public void OnCloseClick()

    {
        ToggleUI();
    }
}
